﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reviewer.Entities;
using MySql.Data.MySqlClient;
using System.Globalization;
using System.Collections.ObjectModel;
using Reviewer.Utilities.Data;
using Reviewer.Data.Bases;

namespace Reviewer.Data
{
    /// <summary>
    /// Writer repository
    /// </summary>
    [CLSCompliant(false)]
    public class WriterRepository : RepositoryCrudAlternativeBaseClass<Writer, Int32, string>
    {
        #region fields (private)
        private const string TABLE_NAME = "V2_Writters";
        private static readonly CrudColumnInfo columnId = new CrudColumnInfo(0, "ID", MySqlDbType.Int32, true, false, false);
        private static readonly CrudColumnInfo columnLoginName = new CrudColumnInfo(1, "Navn", MySqlDbType.VarChar, 255, false, true, false);
        private static readonly CrudColumnInfo columnEmail = new CrudColumnInfo(3, "Email", MySqlDbType.VarChar, 255, false, false, true);
        private static readonly CrudColumnInfo columnAbout = new CrudColumnInfo(4, "OM", MySqlDbType.Text, false, false, true);
        private static readonly CrudColumnInfo columnPlaying = new CrudColumnInfo(5, "Spiller", MySqlDbType.Text, false, false, true);
        #endregion

        #region methods (protected)
        internal override CrudColumnInfo[] Columns
        {
            get
            {
                return new CrudColumnInfo[]{
                   WriterRepository.columnId
                   , WriterRepository.columnLoginName
                   , WriterRepository.columnEmail
                   , WriterRepository.columnAbout
                   , WriterRepository.columnPlaying
                };
            }
        }

        internal override object ColumnToValue(CrudColumnInfo info, Writer item)
        {
            if (info.Equals(WriterRepository.columnId))
                return item.Id;
            else if (info.Equals(WriterRepository.columnLoginName))
                return item.LogOnName;
            else if (info.Equals(WriterRepository.columnEmail))
                return item.Email;
            else if (info.Equals(WriterRepository.columnAbout))
                return item.About;
            else if (info.Equals(WriterRepository.columnPlaying))
                return item.Playing;
            return null;
        }

        protected override string TableName
        {
            get { return WriterRepository.TABLE_NAME; }
        }

        protected override Writer BuildItemFromReader(System.Data.IDataReader reader)
        {
            Writer item = new Writer();
            if (reader != null)
            {
                item.Id = DataReaderUtility.GetValue(reader, reader.GetOrdinal(WriterRepository.columnId.Name), 0);
                item.LogOnName = DataReaderUtility.GetValue(reader, reader.GetOrdinal(WriterRepository.columnLoginName.Name), String.Empty);
                item.Email = DataReaderUtility.GetValue(reader, reader.GetOrdinal(WriterRepository.columnEmail.Name), String.Empty);
                item.About = DataReaderUtility.GetValue(reader, reader.GetOrdinal(WriterRepository.columnAbout.Name), String.Empty);
                item.Playing = DataReaderUtility.GetValue(reader, reader.GetOrdinal(WriterRepository.columnPlaying.Name), String.Empty);
            }
            return item;
        }
        #endregion
    }
}
